package com.zzhrtech.wgzx_cityzen.ui.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.beans.me.CodeBean;
import com.zzhrtech.wgzx_cityzen.beans.task.BaseBean;
import com.zzhrtech.wgzx_cityzen.utils.CountDownButtonHelper;

import java.util.HashMap;
import java.util.Map;

public class ModifyPwdActivity extends BaseActivity {

    EditText et_oldpwd,et_newpwd,et_newpwd2,msmscode;
    Button btn_modify;
    private Button btn_sms;

    private int truesmsCode;
    private String smscode;
    private CountDownButtonHelper buttonHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);
        toolbar.setNavigationIcon(R.mipmap.ic_close);

        initView();
    }
    private void initView(){
        et_oldpwd = (EditText) findViewById(R.id.oldpwd);
        et_newpwd = (EditText) findViewById(R.id.newpwd);
        et_newpwd2 = (EditText) findViewById(R.id.newpwd2);
        btn_modify = (Button) findViewById(R.id.btn_modify);
        btn_sms = (Button) findViewById(R.id.btn_sms);
        msmscode = (EditText) findViewById(R.id.smscode);

        buttonHelper = new CountDownButtonHelper(btn_sms,"获取验证码",60,1);

        btn_sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                httpGetCode();
            }
        });

        btn_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attemptLogin();
            }
        });
    }

    private void httpModify(String oldpass,String newpass){
        Map<String,String> params = new HashMap<String, String>();
        params.put("id",getCache().getAsString(keyUid));
        params.put("oldPass",oldpass);
        params.put("newPass",newpass);

        GsonRequest<BaseBean> gsonRequest = new GsonRequest<BaseBean>(com.android.volley.Request.Method.POST, Constans.HTTP_PASSMODIFY, BaseBean.class, params, new com.android.volley.Response.Listener<BaseBean>() {
            @Override
            public void onResponse(BaseBean response) {

                if (response.isSuccess()){
                    showToast("已修改");
                    finish();

                }else {
                    showToast(response.getMsg());
//                    mDialog.dismiss();
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====",error + "");
//                mDialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }


    private void attemptLogin() {

        // Reset errors.
        et_oldpwd.setError(null);
        et_newpwd.setError(null);
        et_newpwd2.setError(null);
        msmscode.setError(null);

        // Store values at the time of the login attempt.
        String oldpwd = et_oldpwd.getText().toString();
        String newpwd = et_newpwd.getText().toString();
        String newpwd2 = et_newpwd2.getText().toString();
        String smscode = msmscode.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password, if the user entered one.
        if (TextUtils.isEmpty(oldpwd) || !isPasswordValid(oldpwd)) {
            et_oldpwd.setError(getString(R.string.error_invalid_password));
            focusView = et_oldpwd;
            cancel = true;
        }

        // Check for a valid email address.
        if (TextUtils.isEmpty(newpwd) || !isPasswordValid(newpwd)) {
            et_newpwd.setError(getString(R.string.error_invalid_password));
            focusView = et_newpwd;
            cancel = true;
        }

        // Check for a valid email address.
        if (TextUtils.isEmpty(newpwd2) || !isPasswordValid(newpwd2)) {
            et_newpwd2.setError(getString(R.string.error_invalid_password));
            focusView = et_newpwd2;
            cancel = true;
        }


        if (TextUtils.isEmpty(smscode)){
            msmscode.setError("请输入验证码！");
            focusView = msmscode;
            cancel = true;
        }

        if (!smscode.equals(String.valueOf(truesmsCode))){
            msmscode.setError("验证码错误！");
            focusView = msmscode;
            cancel = true;
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
        } else {
            // Show a progress spinner, and kick off a background task to
            // perform the user login attempt.
            httpModify(oldpwd,newpwd);
        }
    }

    private boolean isPasswordValid(String password) {
        //TODO: Replace this with your own logic
        return password.length() > 4;
    }


    private void httpGetCode(){
        Map<String,String> map = new HashMap<String, String>();
        map.put("phone",getCache().getAsString(keyUsername));
        map.put("type","2");

        GsonRequest<CodeBean> gsonRequest = new GsonRequest<CodeBean>(com.android.volley.Request.Method.POST, Constans.HTTP_GETCODE, CodeBean.class, map, new com.android.volley.Response.Listener<CodeBean>() {
            @Override
            public void onResponse(CodeBean response) {

                if (response.isSuccess()) {
                    showToast("获取成功");

                    truesmsCode = response.getData();
                    buttonHelper.start();
                }else {
                    showToast(response.getMsg());
                }

            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.d("result=====",error + "");
//                dialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }
}
